﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QT.Windows.Shell;

namespace LogAnalytics.Edit
{
    public partial class SiteManage : GlassForm
    {
        #region Form

        public SiteManage()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            gbtnRefresh_Click(this.gbtnRefresh, new EventArgs());
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = AeroColor;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this.pSite.Location = new Point((this.pMain.Width - this.pSite.Width) / 2, (this.pMain.Height - this.pSite.Height) / 2);
        }

        #endregion

        #region Event

        #region Main

        #region GlassButton

        private void gbtnRefresh_Click(object sender, EventArgs e)
        {
            loadSiteList();
        }

        private void gbtnAdd_Click(object sender, EventArgs e)
        {
            this.lblTitle.Text = "添加站点配置";
            this.tbSiteName.Text = "";
            this.cbEnable.Checked = true;
            this.lbTelDirectory.Items.Clear();
            this.lbUniDirectory.Items.Clear();
            this.pTop.Enabled = false;
            this.pSite.Visible = true;
            this.pSite.Tag = "";
        }

        private void gbtnModify_Click(object sender, EventArgs e)
        {
            if (this.dgvList.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要修改的站点");
                return;
            }
            if (this.dgvList.SelectedRows.Count > 1)
            {
                MessageBox.Show("每次只能修改一个站点");
                return;
            }

            this.lblTitle.Text = "修改站点配置";
            this.tbSiteName.Text = this.dgvList.SelectedRows[0].Cells[0].Value.ToString();
            this.cbEnable.Checked = this.dgvList.SelectedRows[0].Cells[5].Value.ToString().ToLower() == "false" ? false : true;
            this.lbTelDirectory.Items.Clear();
            if (!string.IsNullOrEmpty(this.dgvList.SelectedRows[0].Cells[1].Value.ToString()))
            {
                foreach (string item in this.dgvList.SelectedRows[0].Cells[1].Value.ToString().Split(','))
                {
                    this.lbTelDirectory.Items.Add(item);
                }
            }
            this.lbUniDirectory.Items.Clear();
            if (!string.IsNullOrEmpty(this.dgvList.SelectedRows[0].Cells[2].Value.ToString()))
            {
                foreach (string item in this.dgvList.SelectedRows[0].Cells[2].Value.ToString().Split(','))
                {
                    this.lbUniDirectory.Items.Add(item);
                }
            }
            this.pTop.Enabled = false;
            this.pSite.Visible = true;
            this.pSite.Tag = this.dgvList.SelectedRows[0].Cells[4].Value.ToString();
        }

        private void gbtnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvList.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的站点");
                return;
            }
            List<string> mySite = new List<string>();
            foreach (DataGridViewRow row in this.dgvList.SelectedRows) mySite.Add(row.Cells[4].Value.ToString());

            try
            {
                int rval = LogBase.SiteManage.Delete(mySite);
                if (rval >= 0)
                {
                    MessageBox.Show(string.Format("共删除{0}条记录", rval));
                }

                gbtnRefresh_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region DataGridList

        private void dgvList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("共{0}条记录", this.dgvList.RowCount);
        }

        #endregion

        #endregion

        #region Panel

        #region GlassButton

        private void gbtnSave_Click(object sender, EventArgs e)
        {
            if (this.tbSiteName.Text.Length == 0)
            {
                MessageBox.Show("请填写 站点名称");
                return;
            }
            if (this.lbTelDirectory.Items.Count == 0 && this.lbUniDirectory.Items.Count == 0)
            {
                MessageBox.Show("请设置 日志目录");
                return;
            }
            if (this.lbTelDirectory.Items.Count == 0 || this.lbUniDirectory.Items.Count == 0)
            {
                string mystr = "";
                if (this.lbTelDirectory.Items.Count == 0) mystr = "电信";
                else mystr = "联通";
                if (MessageBox.Show(string.Format("您还没有设置{0}日志，确认不设置吗？", mystr), "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
            }
            List<string> TelDirectory = new List<string>();
            foreach (object item in this.lbTelDirectory.Items) TelDirectory.Add(item.ToString());
            List<string> UniDirectory = new List<string>();
            foreach (object item in this.lbUniDirectory.Items) UniDirectory.Add(item.ToString());

            int rval=0;
            try
            {
                if (string.IsNullOrEmpty(this.pSite.Tag.ToString())) rval = LogBase.SiteManage.Add(this.tbSiteName.Text, TelDirectory, UniDirectory, this.cbEnable.Checked ? "1" : "0");
                else rval = LogBase.SiteManage.Update(this.pSite.Tag.ToString(), this.tbSiteName.Text, TelDirectory, UniDirectory, this.cbEnable.Checked ? "1" : "0");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            switch (rval)
            {
                case -2:
                    MessageBox.Show("站点名称已存在");
                    return;
                case -1:
                case 0:
                    MessageBox.Show("操作失败");
                    return;
                default:
                    break;
            }

            gbtnRefresh_Click(sender, e);
            this.pTop.Enabled = true;
            this.pSite.Visible = false;
        }

        private void gbtnClose_Click(object sender, EventArgs e)
        {
            this.pTop.Enabled = true;
            this.pSite.Visible = false;
        }

        #endregion

        #region PictureBox

        private void pbTelAdd_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog myFolderDialog = new FolderBrowserDialog();
            if (myFolderDialog.ShowDialog() == DialogResult.Cancel) return;

            string myPath = myFolderDialog.SelectedPath;
            myFolderDialog.Dispose();
            if (!myPath.EndsWith(@"\")) myPath += @"\";
            myPath += @"{0}";

            foreach (object item in this.lbTelDirectory.Items)
            {
                if (item.ToString().ToLower() == myPath.ToLower())
                {
                    MessageBox.Show("目录已存在");
                    return;
                }
            }

            this.lbTelDirectory.Items.Add(myPath);
        }

        private void pbTelDel_Click(object sender, EventArgs e)
        {
            if (this.lbTelDirectory.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择要删除的记录");
                return;
            }

            while (this.lbTelDirectory.SelectedItems.Count > 0)
            {
                this.lbTelDirectory.Items.Remove(this.lbTelDirectory.SelectedItems[0]);
            }
        }

        private void pbUniAdd_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog myFolderDialog = new FolderBrowserDialog();
            if (myFolderDialog.ShowDialog() == DialogResult.Cancel) return;

            string myPath = myFolderDialog.SelectedPath;
            myFolderDialog.Dispose();
            if (!myPath.EndsWith(@"\")) myPath += @"\";
            myPath += @"{0}";

            foreach (object item in this.lbUniDirectory.Items)
            {
                if (item.ToString().ToLower() == myPath.ToLower())
                {
                    MessageBox.Show("目录已存在");
                    return;
                }
            }

            this.lbUniDirectory.Items.Add(myPath);
        }

        private void pbUniDel_Click(object sender, EventArgs e)
        {
            if (this.lbUniDirectory.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择要删除的记录");
                return;
            }

            while (this.lbUniDirectory.SelectedItems.Count > 0)
            {
                this.lbUniDirectory.Items.Remove(this.lbUniDirectory.SelectedItems[0]);
            }
        }

        #endregion

        #endregion

        #endregion

        #region Method

        private void loadSiteList()
        {
            DataTable dt = LogBase.SiteManage.getAll();
            this.dgvList.AutoGenerateColumns = false;
            this.dgvList.Columns[0].DataPropertyName = "SiteName";
            this.dgvList.Columns[1].DataPropertyName = "TelDirectory";
            this.dgvList.Columns[2].DataPropertyName = "UniDirectory";
            this.dgvList.Columns[3].DataPropertyName = "status";
            this.dgvList.Columns[4].DataPropertyName = "GUID";
            this.dgvList.Columns[5].DataPropertyName = "Enable";
            this.dgvList.DataSource = dt;
            this.dgvList.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        #endregion
    }
}
